import ResourceManageSingleton from "../libs/utils/ResourceManageSingleton";
import HeroDTO from "../pojo/dto/HeroDTO";
import HeroPO from "../pojo/po/HeroPO";
import SkillPO from "../pojo/po/SkillPO";
import HeroEntity from "./HeroEntity";

/*****************************
 *@file: HeroManageSingleton
 *@author: 陈吕唐
 *@desc:
 *@date: 2024-02-27	18:23
 *****************************/
export default class HeroManageSingle {
    /****************************************************************************************************************
     *Readonly 常量
     ****************************************************************************************************************/
    private static _single: HeroManageSingle = undefined!;


    /**
     * 英雄表配置数据
     */
    private poMap: Map<number, HeroPO> = new Map<number, HeroPO>();

    /**
     * 技能表配置数据
     */
    private skillPOMap: Map<number, SkillPO> = new Map<number, SkillPO>();


    /**
     * 英雄实体类配置数据
     */
    private entityMap: Map<number, HeroEntity> = new Map<number, HeroEntity>();
    /****************************************************************************************************************
     *Property 定义变量
     ****************************************************************************************************************/
    /****************************************************************************************************************
     * Lifecycle 生命周期相关方法
     ****************************************************************************************************************/
    private constructor() {

    }
    /****************************************************************************************************************
     * Public 公有方法
     ****************************************************************************************************************/
    public init() {
        this.initPO();
    }

    /**
     * 更新英雄数据
     * @param list 
     */
    public updateHeroEntityList(list: HeroDTO[]) {
        for (let i = 0; i < list.length; i++) {
            const element = list[i];
            this.updateHeroEntity(element);
        }
    }

    /**
     * 更新英雄数据
     * @param dto 
     */
    public updateHeroEntity(dto: HeroDTO) {
        let entity = this.entityMap.get(dto.id);
        if (!entity) {
            entity = this.createHeroEntity(dto);
            this.entityMap.set(dto.id, entity);
        }
        entity.updateDTO(dto);
    }
    /****************************************************************************************************************
     * private 私有方法
     ****************************************************************************************************************/
    /**
     * 初始化配置表数据
     */
    private initPO() {
        this.poMap.clear();
        let array: HeroPO[] = ResourceManageSingleton.singleton.getJson(`Character`);
        for (let i = 0; i < array.length; i++) {
            const element = array[i];
            this.poMap.set(element.id, element);
        }

        let skill: SkillPO[] = ResourceManageSingleton.singleton.getJson(`Skill`);
        for (let i = 0; i < skill.length; i++) {
            const element = skill[i];
            this.skillPOMap.set(element.id, element);
        }
    }


    /**
     * 创建英雄实体类
     * @param dto 
     */
    private createHeroEntity(dto: HeroDTO): HeroEntity {
        let po = this.poMap.get(dto.id);
        let vo = { ...po, ...dto };
        let entity = new HeroEntity(vo);
        return entity;
    }
    /****************************************************************************************************************
     * Property Get && Set 属性的 get && set 函数
     ****************************************************************************************************************/
    public static get single() {
        if (!HeroManageSingle._single) {
            HeroManageSingle._single = new HeroManageSingle();
        }
        return HeroManageSingle._single;
    }
}